import React, { Component } from 'react';
import { connect } from 'dva';
import { message, Modal, Pagination, Popconfirm, Button } from 'antd';
import styles from './../Users.css';
import moment from 'moment';
import Factory from '../../factory';
import * as datetime from '../../../utils/datetime'
import dataEditCreate from '../../addons/DataEdit'
import dataTableCreate from '../../addons/DataTable'
import dataImportCreate from '../../addons/DataImport'
const daoName = "DGLSDKDR";
const ButtonGroup = Button.Group;
const columns = [
    { title: '序号', dataIndex: 'XH', key: 'XH', width: 150, sorter: true, dispaly: true, filter: true, export: true },
    { title: '仅贴现业务填此列：金融机构代码（该笔贷款经办机构的统一社会信用代码）', dataIndex: 'JRDM', key: 'JRDM', width: 150, sorter: true, dispaly: true, filter: true, export: true },
    { title: '仅贴现业务填此列：内部机构号（该笔业务经办机构的内部编号）', dataIndex: 'NBJGH', key: 'NBJGH', width: 150, sorter: true, dispaly: true, filter: true, export: true },
    { title: '客户名称', dataIndex: 'KHMC', key: 'KHMC', width: 250, sorter: true, dispaly: true, filter: true, export: true },
    { title: '所属管辖行', dataIndex: 'SSGXH', key: 'SSGXH', width: 200, sorter: true, dispaly: true, filter: true, export: true },
    { title: '所属分行', dataIndex: 'SSFH', key: 'SSFH', width: 180, sorter: true, dispaly: true, filter: true, export: true },
    { title: '新国标行业分类', dataIndex: 'HYFL', key: 'HYFL', width: 250, sorter: true, dispaly: true, filter: true, export: true },
    { title: '企业规模（四部委）', dataIndex: 'QYGM', key: 'QYGM', width: 250, sorter: true, dispaly: true, filter: true, export: true },
    { title: '币种', dataIndex: 'BZ', key: 'BZ', width: 150, sorter: true, dispaly: true, filter: true, export: true },
    { title: '业务品种', dataIndex: 'YWPZ', key: 'YWPZ', width: 250, sorter: true, dispaly: true, filter: true, export: true },
    { title: '合同编号（注：文本格式/贴现业务此处填票号）', dataIndex: 'HTBH', key: 'HTBH', width: 200, sorter: true, dispaly: true, filter: true, export: true },
    { title: '借据编号（注：文本格式/贴现业务此处填票号）', dataIndex: 'JJBH', key: 'JJBH', width: 200, sorter: true, dispaly: true, filter: true, export: true },

    { title: '借据起始日', dataIndex: 'JJQSR', key: 'JJQSR', width: 150, sorter: true, dispaly: true, filter: true, export: true },
    { title: '借据到期日', dataIndex: 'JJDQR', key: 'JJDQR', width: 150, sorter: true, dispaly: true, filter: true, export: true },

    { title: '折人民币余额（万元）', dataIndex: 'YE', key: 'YE', width: 250, sorter: true, dispaly: true, filter: true, export: true },
    { title: '绿色产业指导目录类别（从下拉菜单中选择）', dataIndex: 'MLLB', key: 'MLLB', width: 250, sorter: true, dispaly: true, filter: true, export: true },
    { title: '贷款状态是否为不良（包括次级、可疑、损失）或 关注', dataIndex: 'DKZT', key: 'DKZT', width: 250, sorter: true, dispaly: true, filter: true, export: true },
    { title: '特殊情况备注（如有需填写）', dataIndex: 'QKBZ', key: 'QKBZ', width: 250, sorter: true, dispaly: true, filter: true, export: true },
];
const controls = [
    { name: 'XH', required: false, type: 'text', label: '序号', grid: 12 / 24 },
    { name: 'JRDM', required: false, type: 'text', label: '仅贴现业务填此列：金融机构代码（该笔贷款经办机构的统一社会信用代码）', grid: 12 / 24 },
    { name: 'NBJGH', required: false, type: 'text', label: '仅贴现业务填此列：内部机构号（该笔业务经办机构的内部编号）', grid: 12 / 24 },
    { name: 'KHMC', required: false, type: 'text', label: '客户名称', grid: 12 / 24 },
    { name: 'SSGXH', required: false, type: 'text', label: '所属管辖行', grid: 12 / 24 },
    { name: 'SSFH', required: false, type: 'text', label: '所属分行', grid: 12 / 24 },
    { name: 'HYFL', required: false, type: 'text', label: '新国标行业分类', grid: 12 / 24 },
    { name: 'QYGM', required: false, type: 'text', label: '企业规模（四部委）', grid: 12 / 24 },
    { name: 'BZ', required: false, type: 'text', label: '币种', grid: 12 / 24 },
    { name: 'YWPZ', required: false, type: 'text', label: '业务品种', grid: 12 / 24 },
    { name: 'HTBH', required: false, type: 'text', label: '合同编号（注：文本格式/贴现业务此处填票号）', grid: 12 / 24 },
    { name: 'JJBH', required: false, type: 'text', label: '借据编号（注：文本格式/贴现业务此处填票号）', grid: 12 / 24 },

    { name: 'JJQSR', required: false, type: 'text', label: '借据起始日', grid: 12 / 24 },
    { name: 'JJDQR', required: false, type: 'text', label: '借据到期日', grid: 12 / 24 },

    { name: 'YE', required: false, type: 'text', label: '折人民币余额（万元）', grid: 12 / 24 },
    { name: 'MLLB', required: false, type: 'text', label: '"绿色产业指导目录类别（从下拉菜单中选择）"', grid: 12 / 24 },
    { name: 'DKZT', required: false, type: 'text', label: '贷款状态是否为不良（包括次级、可疑、损失）或 关注', grid: 12 / 24 },
    { name: 'QKBZ', required: false, type: 'text', label: '特殊情况备注（如有需填写）', grid: 12 / 24 },
]
const EditFrom = dataEditCreate(daoName);
const DataTable = dataTableCreate(daoName);
const DataImport = dataImportCreate(daoName);
class DGLSDKDRForm extends Component {
    constructor(props) {
        super(props);
        this.state = {
            dataChange: false
        };
    }
    componentDidMount() {
        this.props.dispatch({
            type: `${daoName}/setKeyName`,
            payload: { keyName: "ID", permID: this.props.permID }
        });
        this.props.dispatch({
            type: `${daoName}/fetch`,
            payload: { daoName, page: 1, pageSize: 10 }
        });
        this.loadOptionData();
    }
    componentWillReceiveProps(nextProps) {
        const { showImport } = this.props
        if (!nextProps.showImport && showImport) {
            this.props.dispatch({
                type: `${daoName}/fetch`,
                payload: { daoName, page: 1, pageSize: 10 }
            });
        }
    }
    loadOptionData() {
        let options = []
        controls.map(c => {
            if (c.fetch) {
                let f = c.fetch;
                if (c.type === 'cascader-select') {
                    f.isCascader = true;
                }
                if (c.type !== 'search') {
                    options.push(f);
                }
            }
        })
        if (daoName && options.length > 0) {
            this.props.dispatch({ type: `${daoName}/fetchOptions`, payload: { options } });
        }
    }
    showImportHandler() {
        this.props.dispatch({
            type: `${daoName}_DataImport/setDisplay`,
            payload: { display: true },
        });
    }
    DownTmportHandler() {
        let columnHeaders = []
        columns.map((c, j) => {
            if (!c.export) {
                return
            }
            let header = {
                Name: c.key,
                Caption: c.title,
                Width: c.width
            }
            columnHeaders.push(header)
        })
        let form = document.createElement('form');//定义一个form表单
        let whereItems = [];
        whereItems.push({
            FieldName: "1",
            Operator: "<>",
            FieldValues: "1"
        })
        const whereCauses = []
        whereCauses.push({
            WhereItems: whereItems,
            RelationSign: "AND"
        })
        const orderItems = []
        form.style = "display:none";
        form.target = "_blank";
        form.method = "post";
        form.action = "/api/ExportXlsx/" + daoName;
        let input1 = document.createElement("input");
        input1.type = "hidden";
        input1.name = "WhereCauses";
        input1.value = JSON.stringify(whereCauses);
        let input2 = document.createElement("input");
        input2.type = "hidden";
        input2.name = "OrderItems";
        input2.value = JSON.stringify(orderItems)
        let input3 = document.createElement("input");
        input3.type = "hidden";
        input3.name = "ColumnHeaders";
        input3.value = JSON.stringify(columnHeaders);
        let input4 = document.createElement("input");
        input4.type = "hidden";
        input4.name = "PermID";
        input4.value = "" + this.props.permID;
        let input5 = document.createElement("input");
        input5.type = "hidden";
        input5.name = "UserID";
        input5.value = "" + this.props.userID;
        document.body.appendChild(form);//将表单放置在web中
        form.appendChild(input1);
        form.appendChild(input2);
        form.appendChild(input3);
        form.appendChild(input4);
        form.appendChild(input5);
        form.submit();
        document.body.removeChild(form)
    }
    render() {
        let { showEdit, record, showImport, permID, popedomTypes } = this.props
        let pTypes = popedomTypes[permID]
        return (
            <div className={styles.normal} style={{ background: '#fff', marginTop: -16, padding: '5px 5px' }}>
                <div>
                    <div className={styles.create}>
                        <ButtonGroup style={{ zIndex: 0 }}>
                            <Button onClick={this.showImportHandler.bind(this)}>导入</Button>
                            <Button onClick={this.DownTmportHandler.bind(this)}>下载模板</Button>
                        </ButtonGroup>
                    </div>
                    <DataTable ref="userTable" daoName={daoName} columns={columns} controls={controls} permID={permID} />
                    <DataImport title="绿色贷款导入" importName="DGLSDKDR" daoName="DGLSDKDR" keys={["ID"]} importMode="TruncteAfterInsert" controls={controls} />
                </div>
            </div>
        )
    }
}

function mapStateToProps(state) {
    const { showEdit, record, selectedRowKeys, keyName } = state[daoName];
    const { userID, orgID, pOrgID, popedomTypes } = state.menus;
    const { display } = state[`${daoName}_DataImport`];
    return {
        userID,
        orgID,
        pOrgID,
        popedomTypes,
        showEdit,
        selectedRowKeys,
        keyName,
        record,
        showImport: display
    }
}
DGLSDKDRForm = connect(mapStateToProps)(DGLSDKDRForm)
export default DGLSDKDRForm;

let render = (props) => React.createElement(DGLSDKDRForm, props);
let f = new Factory()
f.set("DGLSDKDRForm", render)
